import java.util.PriorityQueue;

public class Solution703 {

    PriorityQueue<Integer> priorityQueue=new PriorityQueue<>();
    int size;

    public Solution703(int k, int[] nums) {
        size=k;
        for (int num : nums) {
            priorityQueue.offer(num);
            if (priorityQueue.size()>size){
                priorityQueue.poll();
            }
        }
    }

    public int add(int val) {
        priorityQueue.offer(val);
        if (priorityQueue.size()>size){
            priorityQueue.poll();
        }
        return priorityQueue.peek();
    }

    public static void main(String[] args) {
        Solution703 solution703=new Solution703(3,new int[]{4,5,8,2});
        System.out.println(solution703.add(3));
        System.out.println(solution703.add(5));
        System.out.println(solution703.add(10));
        System.out.println(solution703.add(9));
        System.out.println(solution703.add(4));
    }
}
